package com.cc.crm.service.impl;

import com.cc.crm.entity.SysRight;
import com.cc.crm.entity.SysRole;
import com.cc.crm.entity.SysUser;
import com.cc.crm.entity.SysUserExample;
import com.cc.crm.mapper.SysRoleMapper;
import com.cc.crm.mapper.SysUserMapper;
import com.cc.crm.service.SysUserService;
import com.cc.crm.utils.MD5Util;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 用一句话描述
 * @Auther: 新梦想*陈超
 * @Date: 2020-12-29 14:25
 */
@Service
public class SysUserServiceImpl  implements SysUserService {

    final Logger logger = LogManager.getLogger(SysUserServiceImpl.class.getName());
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public SysUser login(SysUser user) {
        SysUserExample exception = new SysUserExample();
        SysUserExample.Criteria criteria = exception.createCriteria();
        //得到加密后的密码
        String securityPwd = MD5Util.MD5EncodeUtf8(user.getUsrPassword());
        //根据原用户名和加密后密码进行查询用户
        criteria.andUsrNameEqualTo(user.getUsrName())
                .andUsrPasswordEqualTo(securityPwd);
        List<SysUser> sysUsers = sysUserMapper.selectByExample(exception);
        return sysUsers.size()>0?sysUsers.get(0):null;
    }

    @Override
    public String findRoleAndRight(Integer usrRoleId) {
        //1.根据角色id获取 角色的所有信息以及该角色具有的所有权限信息
        SysRole logonRole = sysRoleMapper.findRoleAndRights(usrRoleId);
        //2.拼接用户的操作权限菜单项
        List<SysRight> curRights = logonRole.getCurRights();
        StringBuffer str = new StringBuffer();

        /*

            L01	    ROOT_MENU	Folder	营销管理		营销管理
            L0101	L01	Document	销售机会管理	/ndcrm-sys/sale/list	销售机会管理
            L0102	L01	Document	客户开发计划	${basePath}/sale/list	客户开发计划

         * L01 = theMenu.addChild(l2ID, "Folder", "营销管理", "", "营销管理...");
           L0101 = theMenu.addChild(L01, "Document", "销售机会管理", "~sale/list.html", "销售机会管理...");
           L0102 = theMenu.addChild(L01, "Document", "客户开发计划", "~sale/dev.html", "客户开发计划...");
         */

        String parent = "";
        for (SysRight rt : curRights) {
            if(rt.getRightParentCode().equals("ROOT_MENU")){
                parent = "l2ID";
            }else{
                parent = rt.getRightParentCode();
            }
            str.append(rt.getRightCode());
            str.append(" = theMenu.addChild(");
            str.append(parent);
            str.append(",");
            str.append("\""+rt.getRightType()+"\"");
            str.append(",");
            str.append("\""+rt.getRightText()+"\"");
            str.append(",");
            str.append("\""+rt.getRightUrl()+"\"");
            str.append(",");
            str.append("\""+rt.getRightTip()+"...\"");
            str.append(");");
            str.append("\r\n");

        }
        logger.debug("登录权限菜单结果为：{}",str.toString());
        return str.toString();
    }
}
